package cn.qiyu5522.tk_ems.controller.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.qiyu5522.tk_ems.entity.dto.JobDTO;
import cn.qiyu5522.tk_ems.entity.dto.QueryParamDTO;
import cn.qiyu5522.tk_ems.exception.Assert;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.result.ResponseEnum;
import cn.qiyu5522.tk_ems.retrofit.JobsApi;
import cn.qiyu5522.tk_ems.service.JobService;
import cn.qiyu5522.tk_ems.utils.RedisKey;
import cn.qiyu5522.tk_ems.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ProjectName: tk_ems
 * @Package: cn.qiyu5522.tk_ems.controller.api
 * @ClassName: JobCategoryCotroller
 * @Author: [Lucky Star]
 * @Date: 2023/1/27 21:38
 * @Version: V1.0
 **/
@Api(tags = "职位类别")
@RestController
@Controller(value = "JobCategoryFrontController")
@RequestMapping("/api/jobs")
@SaIgnore
@Slf4j
public class JobCategoryController {

    @Resource
    private JobsApi jobsApi;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private JobService jobService;

    @ApiOperation("获取职位分类")
    @GetMapping("/getjobs")
    public Object getJobs() {
        //先查询redis中是否存在数据列表
        Object result = null;
        try {
            result= redisUtil.get(RedisKey.JOB_CATEGORY_LIST);
            if(result != null){
                log.info("从redis中取值");
                return result;
            }
        } catch (Exception e) {
            log.error("redis服务器异常：" + ExceptionUtils.getStackTrace(e));//此处不抛出异常，继续执行后面的代码
        }
        // 远程调用接口
        // 第三方接口 http://eyasapi.job5156.com/dictionary/job
        log.info("远程调用接口");
        String resultStr = jobsApi.getJobs();
        result = JSON.parseObject(resultStr, Object.class);
        // 将java字符串转为json字符串
        Assert.isNull(result, ResponseEnum.OTHER_API_ERROR);
        //将数据存入redis
        try {
            redisUtil.set(RedisKey.JOB_CATEGORY_LIST,result,RedisKey.EXPIRE_ONE_DAY);
            log.info("数据存入redis");
        } catch (Exception e) {
            log.error("redis服务器异常：" + ExceptionUtils.getStackTrace(e));//此处不抛出异常，继续执行后面的代码
        }
        return result;
    }

    @ApiOperation("工作列表查询")
    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true,dataType ="QueryParamDTO" ,dataTypeClass = QueryParamDTO.class)
    @PostMapping("/positionList")
    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<JobDTO> queryParamDTO) {
        return jobService.queryJobsByCondition(queryParamDTO);
    }
}
